//
//  JiongBoy.m
//  Jiong_Wawa
//
//  Created by lijinxin on 12-2-21.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JiongBoy.h"

@interface MultiAnimateNode : CCAnimate {
    CCSprite* next_sp;
    CCSprite* self_sp;
    CCAnimation *next_am;
    MultiAnimateNode *next;
    int cur_am;
}
@property (readwrite,nonatomic,retain)  CCSprite* next_sp;
@property (readwrite,nonatomic,retain)  CCSprite* self_sp;
@property (readwrite,nonatomic,retain)  CCAnimation *next_am;
@property (readwrite,nonatomic,retain)  MultiAnimateNode *next;
@end

@implementation MultiAnimateNode
@synthesize next_sp;
@synthesize self_sp;
@synthesize next_am;
@synthesize next;

- (void)stop
{
    [super stop];
    self_sp.visible = NO;
    next_sp.visible = YES;
    [next_sp runAction:next];
}

@end

@implementation JiongBoy

- (id)init
{
    return [super init];
}

- (CCSprite*)CreateBoy:(CCLayer*)layer;
{
    if (mHasCreated)
    {
        return NULL;
    }
    CGSize s = [[CCDirector sharedDirector] winSize];
    
    // IMPORTANT:
    // The sprite frames will be cached AND RETAINED, and they won't be released unless you call
    //     [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
    //NOTE:
    //The name of your .png and .plist must be the same name
    CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"yao_anm1.png"];
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"yao_anm1.plist"];
    CCSpriteBatchNode *batchNode2 = [CCSpriteBatchNode batchNodeWithFile:@"yao_anm2.png"];
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"yao_anm2.plist"];
    //
    // Animation using Sprite Sheet
    //
    CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"yao0000.png"]; //"grossini_dance_01.png" comes from your .plist file
    CCSprite *sprite2 = [CCSprite spriteWithSpriteFrameName:@"yao0004.png"];
    sprite.position = ccp( s.width/2, s.height/2); 
    sprite2.position = ccp( s.width/2, s.height/2); 
    sprite.scale = 0.6f;
    sprite2.scale = 0.6f;
    
    [batchNode addChild:sprite];
    [batchNode2 addChild:sprite2];
    [layer addChild:batchNode];
    [layer addChild:batchNode2];
    
    NSMutableArray *animFrames = [NSMutableArray array];
    NSMutableArray *animFrames2 = [NSMutableArray array];
    for(int i = 0; i < 4; i++) {
        
        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"yao%04d.png",i]];
        [animFrames addObject:frame];
        frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"yao%04d.png",i+4]];
        [animFrames2 addObject:frame];
    }
    CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:0.25f];
    CCAnimation *animation2 = [CCAnimation animationWithFrames:animFrames2 delay:0.25f];
    //[sprite2 runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation2 restoreOriginalFrame:NO] ]];
    MultiAnimateNode* ma = [MultiAnimateNode actionWithAnimation:animation restoreOriginalFrame:YES];
    MultiAnimateNode* ma2 = [MultiAnimateNode actionWithAnimation:animation2 restoreOriginalFrame:YES];
    ma.self_sp = sprite;
    ma2.self_sp = sprite2;
    ma.next_sp = sprite2;
    ma2.next_sp = sprite;
    ma.next_am = animation2;
    ma2.next_am = animation;
    ma.next = ma2;
    ma2.next = ma;
    sprite.visible = YES;
    sprite2.visible = NO;
    [sprite runAction:ma];
    mHasCreated = YES;
    return sprite;
}

- (void)DeleteBoy
{
    if (!mHasCreated)
    {
        return;
    }
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
    mHasCreated = NO;
}

- (void)dealloc
{
    [self DeleteBoy];
    return [super dealloc];
}

@end
